package com.awcall.iot.controller;


import com.awcall.iot.common.ResponseResult;
import com.awcall.iot.model.AddressList;
import com.awcall.iot.model.Statements;
import com.awcall.iot.service.IAddressListService;
import com.awcall.iot.service.IStatementsService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.awcall.iot.common.BaseController;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhui
 * @since 2018-03-26
 */
@RestController
@RequestMapping("/admin/addressList")
public class AddressListController extends BaseController {
    
    @Autowired
    private IAddressListService iAddressListService;

    @Autowired
    private IStatementsService iStatementsService;

    @GetMapping("/index")
    public ResponseResult index(AddressList addressList, Page<AddressList> page){

        EntityWrapper<AddressList> entityWrapper = new EntityWrapper<>();

        if (!StringUtils.isEmpty(addressList.getName())){
            entityWrapper.like(AddressList.NAME,addressList.getName());
        }

        entityWrapper.last("ORDER BY CONVERT (NAME USING gbk) ASC");

        Page<AddressList> addressListPage = iAddressListService.selectPage(page, entityWrapper);

        return new ResponseResult<>(addressListPage);
    }

    @GetMapping("/list")
    public ResponseResult list(AddressList addressList){

        EntityWrapper<AddressList> entityWrapper = new EntityWrapper<>();

        if (StringUtils.isEmpty(addressList.getName())){
            entityWrapper.like(AddressList.NAME,addressList.getName());
        }

        List<AddressList> addressLists = iAddressListService.selectList(entityWrapper);

        return new ResponseResult<>(addressLists);
    }

    @GetMapping("/detail")
    public ResponseResult detail(AddressList addressList){

        AddressList addressList1 = addressList.selectById();

        return new ResponseResult<>(addressList1);
    }

    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody AddressList addressList){

        boolean insert = addressList.insert();

        if (insert) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody AddressList addressList){

        boolean updateById = addressList.updateById();

        if (updateById) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody AddressList addressList){

        boolean deleteById = addressList.deleteById();

        if (deleteById) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }
}
